package com.sinosoft.lis.vschema;

import com.sinosoft.lis.schema.DisplaySchema;
import com.sinosoft.utility.SchemaSet;
import com.sinosoft.utility.SysConst;

public class DisplaySet extends SchemaSet{
	
	public boolean add(DisplaySchema aSchema){
		return super.add(aSchema);
	}
	
	public boolean add(DisplaySet aSet){
		return super.add(aSet);
	}
	
	public boolean remove(DisplaySchema aSchema){
		return super.remove(aSchema);
	}
	
	public DisplaySchema get(int index){
		DisplaySchema tSchema = (DisplaySchema)super.getObj(index);
		return tSchema;
	}
	
	public boolean set(int index,DisplaySchema aSchema){
		return super.set(index, aSchema);
	}
	
	public boolean set(DisplaySet aSet){
		return super.set(aSet);
	}
	
	public String encode(){
		StringBuffer strReturn = new StringBuffer("");
		int n = this.size();
		for(int i = 0;i <= n;i++){
			DisplaySchema aSchema = this. get(i);
			strReturn.append(aSchema.encode());
			if(i != n){
				strReturn.append(SysConst.RECORDSPLITER);	
			}
		}
			
			return strReturn.toString();
	}
	
	public boolean decode(String str){
		
		int nBeginPos = 0;
		int nEndPos = str.indexOf('^');
		this.clear();
		
		while(nEndPos != -1){
			DisplaySchema aSchema = new DisplaySchema();
			if(aSchema.decode(str.substring(nBeginPos,nEndPos))){
				
				this.add(aSchema);
				nBeginPos = nEndPos + 1;
				nEndPos = str.indexOf('^', nEndPos + 1);
			}else{
				this.mErrors.copyAllErrors(aSchema.mErrors);
				return false;
			}
		}
		
		DisplaySchema tSchema = new DisplaySchema();
		if(tSchema.decode(str.substring(nBeginPos))){
			this.add(tSchema);
			return true;
		}else{
			this.mErrors.copyAllErrors(tSchema.mErrors);
			return false;
		}
	}
}
